<template>
  <!-- 添加管理员弹框 -->
  <div class="dialog-content">
    <el-dialog v-model="state.isShowCreatAdminDialog" width="400px" :title="`添加${dialogTitle}`">
      <el-form class="login-form" :label-position="labelPosition" :rules="rules" :model="formData">
        <el-form-item label="账号" prop="account">
          <el-input v-model="formData.account" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" show-password />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="formData.sex" placeholder="请选择性别">
            <el-option label="男" value="男" />
            <el-option label="女" value="女" />
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="部门" prop="department">
          <el-select v-model="formData.department" placeholder="请选择部门">
            <el-option v-for="(item, index) in props.departmentList" :key="index" :label="item"
              :value="item" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="state.isShowCreatAdminDialog = false">取消</el-button>
          <el-button type="primary" @click="createAdmin">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { createAdminAPI } from '../../../api/userinfo'
import { tracking } from '../../../utils/operate.js'


interface Department {
  department: string
}

const labelPosition = ref('top')
const props = defineProps<{
  getFirstPage: Function,
  departmentList: Department[]
}>()



const Titile = reactive([
  { id: 1, title: '产品管理员' },
  { id: 2, title: '消息管理员' },
  { id: 3, title: '用户管理员' },
])
const dialogTitle = ref()

const state = reactive({
  isShowCreatAdminDialog: false,
})



interface formData {
  account: number | null,
  password: string,
  name: string,
  sex: string,
  email: string,
  department: string
  identity: string
}

const formData: formData = reactive({
  account: null,
  password: '',
  name: '',
  sex: '',
  email: '',
  department: '',
  identity: ''
})


// 表单校验
const rules = reactive({
  account: [
    { required: true, message: '请输入管理员的注册账号', trigger: 'blur' },
    { pattern: /^[0-9]{6,10}$/, message: '账号必须为 6 - 10 位数字', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入管理员的注册密码', trigger: 'blur' },
    { pattern: /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z\d]+$/ , message: '密码必须是数字加字母形式', trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入管理员的名字', trigger: 'blur' },
    { pattern: /^[\u4e00-\u9fa5]{2,}$/, message: '名字必须为 2 个汉字以上', trigger: 'blur' }
  ],
  sex: [
    { required: true, message: '请输入管理员的性别', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入管理员的邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  department: [
    { required: true, message: '请输入管理员的部门', trigger: 'blur' }
  ]
});


// 打开弹框
const openDialog = (id) => {
  state.isShowCreatAdminDialog = true
  dialogTitle.value = Titile[id - 1].title
  formData.identity = dialogTitle.value
}

// 创建管理员
const createAdmin = async () => {

  const res = await createAdminAPI(formData)

  if (res.data.status === 0) {
    ElMessage({
      message: res.data.message,
      type: 'success'
    })
    state.isShowCreatAdminDialog = false
    if (props.getFirstPage) {
      props.getFirstPage()
    }
    tracking("用户管理", `创建了${formData.identity}`, formData.name , "中级")
  } else {
    ElMessage({
      message: res.data.message,
      type: 'error'
    })
  }

}

defineExpose({
  openDialog
})


</script>

<style lang="scss" scoped>
.dialog-content {
  display: flex;
  padding: 20px 30px;
}

:deep(.el-form-item) {
  margin: 30px;
}
</style>